package com.seek.inter;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.seek.common.handler.SpringContextHolder;
import com.seek.model.dto.ResultInfo;
import com.seek.model.entity.TSmsLog;
import com.seek.service.ReadService;
import com.seek.service.SmsLogService;
import com.seek.util.CommonUtil;
import com.seek.util.ConstantsUtil;
import com.seek.util.SmsCodeCacheUtil;
import com.seek.util.SmsSendUtil;

/**
 * 获取短信验证api
 *
 */
public class SmsInter {
	
	private SmsLogService smsLogService = (SmsLogService)SpringContextHolder.getBean("smsLogService");
	private ReadService readService = (ReadService)SpringContextHolder.getBean("readService");
	
	/**
	 * 获取验证码api
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getSmscodeNoCheck(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		String phoneNo = reqJson.getString("phoneNo");
		String type = reqJson.getString("type");
		String companyId = reqJson.getString("companyId");

		// 取得验证码
		String code = SmsSendUtil.generateSmsCode();
		// 封装短信验证码的内容
		Map<String, String> data = new HashMap<String, String>();
		data.put("code", code);

		boolean b;
		// 发送验证码
		b = SmsSendUtil.sendSms(phoneNo, SmsSendUtil.TYPE_OF_VERIFICATION, data);

		if (!b){
			result.setStatus(1);
			result.setMessage("短信发送失败！"); 
			return result;
		}else{
			// 保存t_sms_log
			TSmsLog log = new TSmsLog();
			log.setCompanyId(CommonUtil.getLongValue(companyId));
			log.setCreateTime(new Date());
			log.setMsgChannel(ConstantsUtil.SMS_CHANNAL_SUBMAIL);
			log.setMsgType(CommonUtil.getIntValue(type));
			log.setPhoneNo(phoneNo);
			log.setStatus(0);
			log.setVerifCode(code);
			smsLogService.save(log);
			
			// 缓存验证码
			SmsCodeCacheUtil.put(phoneNo, code);
			result.setStatus(0);
			result.setMessage("成功");
			return result;
		}
	}
	
	/**
	 * 获取短信验证码:分各种不同情况
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public ResultInfo getSmscode(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		String phoneNo = reqJson.getString("phoneNo");
		String type = reqJson.getString("type");
		// type == 1 app注册，如果该手机号已注册，则直接返回
		// type == 2 app忘记密码，如果该手机号未注册，则直接返回
		// type == 3 后台忘记密码，如果该手机号对应的用户不存在，则直接返回
		String sql = "";
		if ("1".equals(type)){
			sql = "select 1 cnt from t_customer where phone_no=?";
			List ds = readService.findMapBySql(sql, new Object[]{phoneNo});
			if (ds != null && ds.size()>0){
				result.setStatus(1);
				result.setMessage("该手机号已注册，请重新输入手机号");
				return result;
			}
		}else if ("2".equals(type)){
			sql = "select 1 cnt from t_customer where phone_no=?";
			List ds = readService.findMapBySql(sql, new Object[]{phoneNo});
			if (ds == null || ds.size()<1){
				result.setStatus(1);
				result.setMessage("该手机号未注册，请重新输入手机号");
				return result;
			}
		}else if ("3".equals(type)){
			sql = "select 1 cnt from t_sys_user where phone_no=?";
			List ds = readService.findMapBySql(sql, new Object[]{phoneNo});
			if (ds == null || ds.size()<1){
				result.setStatus(1);
				result.setMessage("该手机号对应的用户不存在，请重新输入手机号");
				return result;
			}
		}
			
		return getSmscodeNoCheck(request, reqJson);
	}
}
